import java.awt.BorderLayout;
import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 * 
 * @author Vit Terl
 *
 */
public class NotationPanel extends JPanel{
	private static final long serialVersionUID = 1L;
	
	private static NotationPanel instance;
	
	private JTable table;
	private JScrollPane sPanel;
	public static ArrayList<Mark> list;
	public static String[][] data = new String[115][3];

	MyTableModel tm;
	
	private boolean onMove = true;
	private int tah = 0;
	private int tah2 = 0;
	
	/**
	 * Konstruktor
	 */
	private NotationPanel() {
		super(true);
		setLayout(new BorderLayout());  
		
		table = new JTable();
		sPanel = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		
		add(sPanel, BorderLayout.CENTER);
		
	}
	/**
	 * vytvoreni prave jedny instance
	 * @return
	 */
	public static NotationPanel getInstance() {
		if(instance == null) 
			instance = new NotationPanel();
		
		return instance;
	}
	/**
	 * Vykresli NotationPanel
	 */
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		list = MoveList.getInstance().getAllMoves();
		String[] colNames = {"Move", "Black", "White"};
		
			try {
				
				if (onMove == true) {
					data[tah][0] = "" + (tah + 1);
					data[tah][1] = "" + list.get(tah2);	
					tah2++;
				} else {
					data[tah][2] = "" + list.get(tah2);	
					tah++;
					tah2++;
				}
				if (Move.isWin() == true) {
					if (onMove == true) {
						data[tah + 1][1] = "WIN"; 			
						data[tah + 1][2] = "LOSE"; 
					} else {
						data[tah][2] = "WIN"; 
						data[tah][1] = "LOSE"; 
					}
				}
				if (Move.draw == true){
					data[tah + 1][1] = "DRAW"; 			
					data[tah + 1][2] = "DRAW"; 
				}
				
				onMove = !onMove;
			} catch (Exception e) {
				//e.printStackTrace();
			}						
		
		tm = new MyTableModel(data, colNames);
		table.setModel(tm);
		
		table.getColumnModel().getColumn(0).setMinWidth(5);
		table.getColumnModel().getColumn(1).setMinWidth(5);
		table.getColumnModel().getColumn(2).setMinWidth(5);
		

		table.getColumnModel().getColumn(0).setMaxWidth(500);
		table.getColumnModel().getColumn(1).setMaxWidth(500);
		table.getColumnModel().getColumn(2).setMaxWidth(500);
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);

		tm.fireTableDataChanged();
		
		
	}
	
	public int getTah() {
		return tah;
	}

	public void setTah(int tah) {
		this.tah = tah;
	}

	public int getTah2() {
		return tah2;
	}

	public void setTah2(int tah2) {
		this.tah2 = tah2;
	}
	
	public boolean isOnMove() {
		return onMove;
	}

	public void setOnMove(boolean onMove) {
		this.onMove = onMove;
	}
	/**
	 * Vlastni trida pro navrch tabulky
	 * @author Vit Terl
	 *
	 */
	public class MyTableModel extends DefaultTableModel{
		private static final long serialVersionUID = 1L;

		public MyTableModel(Object[][] tableData, Object[] colNames) {
		      super(tableData, colNames);
		   }

		   public boolean isCellEditable(int row, int column) {
		      return false;
		   }
	}
}